package flirtme.server;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.widget.Toast;

import com.ans.flirtme.R;
import com.google.android.gcm.GCMRegistrar;
import com.loopj.android.http.RequestParams;

import flirtme.activities.LogInActivity;
import flirtme.constants.Constants;
import flirtme.libraries.utils.Utils;
import flirtme.managers.UserManager;
import flirtme.widgets.NonCancellableProgressDialog;


public class LoginRegisterServices {
	
	static String userName;
	static String password;
	static String registerId;
	
	public static void callServiceSelected (final Context context, String serviceSelected, String username, String pass, String regId) {
		userName = username;
		password = pass;
		registerId = regId;
		
		 if (serviceSelected.equalsIgnoreCase(Constants.LOGIN_SERVICE)) {
				callLoginService(context);
		 }
		else if (serviceSelected.equalsIgnoreCase(Constants.REGISTER_SERVICE)) {
				callRegisterService(context);
		}
		else if (serviceSelected.equalsIgnoreCase(Constants.REGISTER_ID_SERVICE)) {
			callRegisterIDService(context);
	} 
	}
	
	private static void callRegisterIDService(Context context) {
		RequestParams params = new RequestParams();

		params.put("username", userName);
		params.put("gcm_regid", registerId);
		callRegisterServiceService(context, params);
	}

	private static void callLoginService(final Context context) {
		RequestParams params = new RequestParams();

		params.put("username", userName);
		params.put("password", password);
		params.put(Constants.USER_GCM_REG_ID, registerId);
		
		UserManager.instance().setProfileCreated(true); //Esto no es muy lindo, pero si necesario
		
		callService(context, Constants.LOGIN_SERVICE, params,
				R.string.login_successful, R.string.login_fail);
	}
	private static  void callRegisterService(final Context context) {
		RequestParams params = new RequestParams();

		params.put("username", userName);
		params.put("password", password);
		params.put(Constants.USER_GCM_REG_ID, registerId);
		
		UserManager.instance().setProfileCreated(false); //Esto no es muy lindo, pero si necesario

		callService(context, Constants.REGISTER_SERVICE, params,
				R.string.register_successful, R.string.register_fail);
	}
	
	private static  void callService(final Context context, final String serviceName, RequestParams params,
			final int successMessage, final int failMessage) {

		FlirtMeRestClient.get(serviceName, params,

		new FlirtMeAsyncHttpResponseHandler(context) {
			NonCancellableProgressDialog dialog = new NonCancellableProgressDialog(
					context);
			@Override
			public void onSuccess(JSONObject response) {
				if (FlirtMeAsyncHttpResponseHandler.isValid(response)) {

					toastMessageWithText(context, successMessage);
					this.parseResponse (response, context);
				} else {
					
					toastMessageWithText(context, failMessage);
				}
			}

			private void parseResponse(JSONObject response, Context context) {
				
				
					HashMap<String, String> userMap = new HashMap<String, String>();

					userMap.put(Constants.USER_NAME, userName);
					userMap.put(Constants.PASSWORD_KEY, password);

					try {

						JSONObject payload = response
								.getJSONObject(Constants.PAYLOAD);

						String useriD = payload.getString(Constants.USER_ID);

						userMap.put(Constants.USER_ID, useriD);

					} catch (JSONException e) {
						e.printStackTrace();
					}

					UserManager.instance().createUser(userMap);
					LogInActivity.showNextPage(context);	
				
				
				
			}

			@Override
			public void onFinish() {
				dialog.dismiss();
			}

			@Override
			public void onStart() {
				dialog.setMessage(context.getString(R.string.calling_service_dialog_msg));
				dialog.show();

			}
		});
	}

	private static  void callRegisterServiceService(final Context context, RequestParams params) {

		FlirtMeRestClient.get(Constants.REGISTER_ID_SERVICE, params,

		new FlirtMeAsyncHttpResponseHandler(context) {
		
			@Override
			public void onSuccess(JSONObject response) {
				if (FlirtMeAsyncHttpResponseHandler.isValid(response)) {
					GCMRegistrar.setRegisteredOnServer(context, true);
					toastMessageWithText(context, R.string.register_gcm_successful);
					
				} else {
					toastMessageWithText(context, R.string.register_fail);
				}
			}
		});
	}
	
	private static void toastMessageWithText(Context context, int text) {
		Toast.makeText(context, text, Toast.LENGTH_LONG).show();
	}
}
